package com.seaside.park.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seaside.common.bean.Result;
import com.seaside.parkCommon.bean.*;
import com.seaside.parkCommon.mapper.CommemorateMapper;
import com.seaside.parkCommon.mapper.UserMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;

@Api(tags="纪念日")
@RestController
@RequestMapping("/api/commemorate")
public class CommemorateAPI {

    @Autowired
    private CommemorateMapper commemorateMapper;
    @Autowired
    private UserMapper userMapper;

    @ApiOperation(value = "纪念日添加", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "bearer", dataTypeClass = String.class, required = true, value = "用户token"),
            @ApiImplicitParam(paramType = "query", name = "parkPlace", dataTypeClass = ParkPlace.class, required = true, value = "纪念日信息")
    })
    @PostMapping("/add")
    public Result add(Commemorate commemorate, @ApiIgnore Authentication auth) {
        commemorate.setUserName(auth.getName());
        commemorate.setCreateTime(new Date());
        if (commemorateMapper.insert(commemorate) > 0) {
            return Result.OK("添加成功");
        } else {
            return Result.Error("添加错误");
        }
    }
    @ApiOperation(value = "纪念日修改", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "bearer", dataTypeClass = String.class, required = true, value = "用户token"),
            @ApiImplicitParam(paramType = "query", name = "parkPlace", dataTypeClass = ParkPlace.class, required = true, value = "纪念日信息")
    })
    @PostMapping("/update")
    public Result update(Commemorate commemorate, @ApiIgnore Authentication auth) {
        commemorate.setUserName(auth.getName());
        commemorate.setUpdateTime(new Date());
        if (commemorateMapper.updateById(commemorate) > 0) {
            return Result.OK("修改成功");
        } else {
            return Result.Error("修改错误");
        }
    }
    @ApiOperation(value = "纪念日删除", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "bearer", dataTypeClass = String.class, required = true, value = "用户token"),
            @ApiImplicitParam(paramType = "query", name = "parkPlace", dataTypeClass = ParkPlace.class, required = true, value = "纪念日信息")
    })
    @PostMapping("/remove/{id}")
    public Result add(@PathVariable String id, @ApiIgnore Authentication auth) {
        if (commemorateMapper.deleteById(id) > 0) {
            return Result.OK("删除成功");
        } else {
            return Result.Error("删除错误");
        }
    }

    @ApiOperation(value = "纪念日列表", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="header",name="bearer",dataTypeClass=String.class,required=true,value="用户token"),
            @ApiImplicitParam(paramType="query",name="curpage",dataTypeClass=Integer.class,required=true,value="当前页")
    })
    @PostMapping("/list")
    public Result<IPage<Commemorate>> list(@ApiIgnore Authentication auth, int curpage) {

        Page<Commemorate> page = new Page<>(curpage, 15, false);

        User user = userMapper.selectById(auth.getName());
        if (user == null) {
            return Result.Error("未找到该用户！");
        }

        QueryWrapper<Commemorate> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        IPage<Commemorate> iPage = commemorateMapper.selectPage(page, queryWrapper);
        return Result.OK("操作成功",iPage);
    }
}
